/**
 * Project Name:fastPlat
 * File Name:ArticleCommentController.java
 * Package Name:com.fast.cms.web.controller.article
 * Date:2014年12月17日下午3:55:40
 * Copyright (c) 2014, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.cms.web.controller.article;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.model.AnoSysLog;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.db.page.PageView;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.model.article.ArticleComment;
import com.fast.cms.service.article.ArticleCommentService;
import com.fast.cms.web.form.ArticleCommentForm;
import com.fast.common.userCache.UserRequestUtils;

/**
 * ClassName:ArticleCommentController <br/>
 * 用途: 文章评论控制 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  
 * @since    JDK 1.6
 * @see
 * Date:     2014年12月17日 下午3:55:40 <br/> 	 
 */
@Controller
@RequestMapping("/articleComment")
public class ArticleCommentController extends BaseController{
	
	@Resource(name="articleCommentService")
	private ArticleCommentService articleCommentService;

	/**
	 * 编辑[ArticleCommentService]信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editArticleComment.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑文件信息")
	public ResponseData editArticleCommentService(){
		ResponseData responseData = new ResponseData();
		ArticleComment articleComment = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				articleComment = this.articleCommentService.getById(ArticleComment.class,id);
				articleComment = (ArticleComment) webUtil.bindDataToBean(articleComment);
			}else{
				articleComment = new ArticleComment();
				articleComment = (ArticleComment) webUtil.bindDataToBean(articleComment);
				UserRequestUtils.bindEditorInfo(articleComment);
			}
			UserRequestUtils.bindCreaterInfo(articleComment);
			if(this.articleCommentService.merge(articleCommentService)!=null){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除[ArticleComment]信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delArticleCommentByIdForHS.do")
	@AnoSysLog(actionName="删除文件到回收站")
	public ResponseData delArticleCommentByIdForHS(int id) {
		LogUtils.info("delArticleCommentByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(ArticleComment.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = articleCommentService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delArticleCommentByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除[ArticleComment]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delArticleCommentById.do")
	@AnoSysLog(actionName="彻底删除某个文件")
	public ResponseData delArticleCommentById(int id) {
		LogUtils.info("delArticleCommentById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = articleCommentService.deleteById(ArticleComment.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delArticleCommentById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个[ArticleComment]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelArticleComment.do")
	@AnoSysLog(actionName="批量删除文章评论")
	public ResponseData batchDelArticleComment(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = articleCommentService.deleteByIds(ArticleComment.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 分页获取评论列表
	 * @param adminForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getArticleCommentList.do")
	public Map<String,Object> getArticleCommentListByPage(ArticleCommentForm articleCommentForm) {
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<ArticleComment> pageView = new PageView<ArticleComment>();
		try {
			pageView = this.articleCommentService.getArticleCommentListByPage(articleCommentForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return gridMap;
	}
	
	
	
	/**
	 * 获取评论信息详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/js/getArticleCommentDetail.do")
	@AnoSysLog(actionName="获取评论信息详情")
	public ResponseData getAjaxArticleCommentDetail(int id) {
		LogUtils.info("getAjaxArticleCommentDetail is comming:"+id);
		ResponseData responseData = new ResponseData();
		try {
			ArticleComment articleComment = articleCommentService.getById(ArticleComment.class,id);
			responseData.setData(articleComment);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.info("获取评论信息详情getArticleCommentDetail err",e);
			responseData.failue();
		}
		return responseData;
	}
	
	
	/**
	 * 查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getArticleCommentDetail.do")
	public ModelAndView getArticleCommentDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		ArticleComment bean = articleCommentService.queryForBeanById(ArticleComment.class, id);
		try {
			mv.addObject("articleComment",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/articleComment/articleComment-add.jsp");
		}else{
			mv.setViewName("/adminMain/articleComment/articleComment-detail.jsp");
		}
		return mv;
	}
}

